package com.ayf.payment.game.api.controller.paychannel;

import com.alibaba.fastjson.JSONObject;
import com.ayf.payment.game.api.dto.sign.MerchantTwoApplyAsyncReqDTO;
import com.ayf.payment.game.api.service.sign.MerchantTwoSignService;
import com.ayf.payment.game.api.utils.SnowFlakeUtils;
import com.swwx.charm.commons.lang.utils.LogPortal;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.LinkedHashMap;

/**
 * @Author: zh-liang
 * @Date: 2022-08-17 17:55
 */
@RequestMapping("/n/zfy/merchant")
@RestController
public class ZFYMerchantNotifyController {

    @Autowired
    private MerchantTwoSignService merchantTwoSignService;

    /**
     * 异步通知处理
     */
    @RequestMapping("/notify")
    public void notify(HttpServletRequest request, HttpServletResponse response) {
        String traceId = SnowFlakeUtils.getTraceId();
        PrintWriter pw = null;
        String Code = request.getParameter("Code");
        String Msg = request.getParameter("Msg");
        String MerId = request.getParameter("MerId");
        String UserId = request.getParameter("UserId");
        String Status = request.getParameter("Status");
        String Sign = request.getParameter("Sign");
        LinkedHashMap<String, Object> resData = new LinkedHashMap<>();
        resData.put("Code", Code);
        resData.put("Msg", Msg);
        resData.put("MerId", MerId);
        resData.put("UserId", UserId);
        resData.put("Status", Status);
        resData.put("Sign", Sign);
        LogPortal.info("{} 支付云-商户申请-异步通知 接收到的参数为：{}", traceId, JSONObject.toJSONString(resData));
        try {
            pw = response.getWriter();
            if (StringUtils.isEmpty(UserId)) {
                LogPortal.info("{} 支付云-商户申请-异步通知 商户号数值为空，不进行后续操作", traceId);
                pw.write("ERROR商户号数值为空");
                pw.flush();
                return;
            }
            MerchantTwoApplyAsyncReqDTO req = new MerchantTwoApplyAsyncReqDTO();
            req.setData(resData);
            req.setChannelCode("ZFYFZ");
            req.setTraceId(traceId);
            boolean boo = merchantTwoSignService.merchantTwoApplyAsyncExec(req);
            if (boo) {
                LogPortal.info("{} 支付云-商户申请-异步通知 处理成功", traceId);
                pw.write("OK");
                pw.flush();
                return;
            }
            LogPortal.info("{} 支付云-商户申请-异步通知 处理失败", traceId);
            pw.write("处理失败");
            pw.flush();
        } catch (Exception e) {
            LogPortal.error("{} 支付云-商户申请-异步通知 处理失败 商户号:[{}]，具体原因:{}", e, traceId, MerId, e.getMessage());
        } finally {
            if (pw != null) {
                pw.close();
            }
        }
    }

}
